<?php

abstract class tx_zlib_abstract_viewhelper
{

    /**
     * @var tx_zlib_view
     */
    protected $view;

    /**
     * @var tslib_pibase
     */
    protected $pi;

	/**
	 * @var string
	 */
	protected $idDelim = '_';

	/**
	 * @var string
	 */
	protected $classDelim = '-';

    /**
     * @param tx_zlib_view $view
     */
    public function setView(tx_zlib_view $view)
    {
        $this->view = $view;
    }

    /**
     * @return tx_zlib_view
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param tslib_pibase $pi
     */
    public function setPi(tslib_pibase $pi)
    {
		$this->pi = $pi;
    }

    /**
     * @return tslib_pibase
     */
    public function getPi()
    {
        return $this->pi;
    }

    /**
     * @return tx_zlib_viewhelper
     */
    public function getHelper()
    {
    	return $this->view->getHelper();
    }

    /**
     * Converts an associative array to a string of tag attributes.
     *
     * @access public
     *
     * @param array $attribs From this array, each key-value pair is
     * converted to an attribute name and value.
     *
     * @return string The XHTML for the attributes.
     */
    protected function htmlAttribs($attribs)
    {
        $xhtml = '';
        foreach ((array) $attribs as $key => $val) {
            $key = $this->view->getHelper()->escape($key);

            if (is_array($val)) {
                $val = implode(' ', $val);
            }
            $val = $this->view->getHelper()->escape($val);

            if ('id' == $key) {
                $val = $this->normalizeId($val);
            }

            if (strpos($val, '"') !== false) {
                $xhtml .= " $key='$val'";
            } else {
                $xhtml .= " $key=\"$val\"";
            }

        }
        return $xhtml;
    }

    /**
     * Normalize an ID
     *
     * @param  string $value
     * @return string
     */
    protected function normalizeId($value)
    {
        if (strstr($value, '[')) {
            if ('[]' == substr($value, -2)) {
                $value = substr($value, 0, strlen($value) - 2);
            }
            $value = trim($value, ']');
            $value = str_replace('][', $this->idDelim, $value);
            $value = str_replace('[', $this->idDelim, $value);
        }
        return $value;
    }

    /**
     * Normalize a class attribute
     *
     * @param  string $value
     * @return string
     */
    protected function normalizeClass($value)
    {
        if (strstr($value, '[')) {
            if ('[]' == substr($value, -2)) {
                $value = substr($value, 0, strlen($value) - 2);
            }
            $value = trim($value, ']');
            $value = str_replace('][', $this->classDelim, $value);
            $value = str_replace('[', $this->classDelim, $value);
        }
        return $value;
    }

    /**
     * @param string $delim
     * @return tx_zlib_viewhelper_form
     */
	public function setIdDelim($delim)
	{
		$this->idDelim = $delim;
		return $this;
	}

    /**
     * @param string $delim
     * @return tx_zlib_viewhelper_form
     */
	public function setClassDelim($delim)
	{
		$this->classDelim = $delim;
		return $this;
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_abstract_viewhelper.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_abstract_viewhelper.php']);
}
